package com.thenmorethings.action;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.thenmorethings.entity.Admin;
import com.thenmorethings.service.AdminService;

public class AdminAction extends ActionSupport{

	

	@Resource
	private AdminService adminService;

	private Admin admin;
	public String loginUI()
	{
		return "loginUI";
	}
	
	
	public String login()
	{
		if(StringUtils.isNotBlank(admin.getUserName()) && StringUtils.isNotBlank(admin.getPwd()))
		{
			Admin ad = adminService.login(admin);
			if(ad!=null)
			{
				//将用户信息放入session中
				ServletActionContext.getRequest().getSession().setAttribute("loginSession", ad);
				
				//将用户登录记录到日志文件
				HttpSession session = ServletActionContext.getRequest().getSession();
				Object obj = session.getAttribute("loginSession");
				
				
				//重定向跳转到首页
				return "home";

			}
			else
			{
				return "error";
			}
		}
		else
		{
			return "error";
		}
		
	}
	
	public String loginOut()
	{
		ServletActionContext.getRequest().getSession().removeAttribute("loginSession");
		return "loginOut";
	}


	public Admin getAdmin() {
		return admin;
	}


	public void setAdmin(Admin admin) {
		this.admin = admin;
	}
	
	
	
}
